/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apus.command.period;

import apus.command.AbstractCommand;
import apus.entity.RealPeriod;
import apus.exception.BusinessException;
import apus.persistence.PeriodDAO;
import apus.services.ServiceLocator;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

/**
 * The <code>AddPeriodCommand</code> class represents command for adding new 
 * period to the database.
 * @author  Maxim Vasilevsky
 * @author  Roman Dyatkovsky
 * @since APUS v0.4
 * @see AbstractCommand
 */
public class AddPeriodCommand extends AbstractCommand{

    private final static String START_DATE = "Start date";
    private final static String END_DATE = "End date";
    
    private final static String[] paramsNames = { START_DATE, END_DATE };
    
    DateFormat df = DateFormat.getDateInstance();
    
    public AddPeriodCommand() {
        super("Create period");
    }
    
    @Override
    public void execute() {
        
        PeriodDAO periodDAO = ServiceLocator.getService(PeriodDAO.class);

        
        
        try {
            if (df.parse(params.get(START_DATE)).after(df.parse(params.get(END_DATE))) ){
                throw new BusinessException("Start date must be earlier than end date");
            }
            periodDAO.create(new RealPeriod(params.get(START_DATE), params.get(END_DATE)));
        } catch (ParseException ex) {
            throw new BusinessException("Wrong date format. Use dd.mm.yyyy ");
        }
    }
    
    @Override
    public List<String> getParamNames() {
        return Arrays.asList(paramsNames);
    }
    
}